//
//  HLUpdateTipView.m
//  YueYue
//
//  Created by hulu001 on 2023/9/13.
//

#import "HLUpdateTipView.h"
#import "HLUserOptionRequest.h"

@interface HLUpdateTipView ()

@property (nonatomic, strong) QMUIButton *okButton;
@property (nonatomic, strong) QMUIButton *sureButton;
@property (nonatomic, strong) QMUIButton *cancelButton;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UIImageView *backView;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UILabel *versionLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation HLUpdateTipView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI
{
    
    self.backgroundColor = UIColorClear;
    self.containerView = [[UIView alloc] init];
    self.containerView.layer.masksToBounds = YES;
    self.containerView.layer.cornerRadius = 20;
    self.containerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_containerView];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(20);
    }];
    
    self.backView = [[UIImageView alloc] init];
    _backView.image = UIImageMake(@"update_back");
    _backView.contentMode = UIViewContentModeScaleAspectFill;
    [_containerView addSubview:_backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.iconView = [[UIImageView alloc] init];
    _iconView.image = UIImageMake(@"update_icon");
    [self addSubview:_iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
    }];

    
    self.versionLabel = [[UILabel alloc] init];
    _versionLabel.text = @"v1.0.12";
    _versionLabel.textColor = HLTextColor2;
    _versionLabel.font = UIFontMake(14);
    _versionLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_versionLabel];
    [_versionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.equalTo(_iconView.mas_bottom).offset(4);
    }];
    
    self.subTitleLabel = [[UILabel alloc] init];
    _subTitleLabel.text = @"更新内容：";
    _subTitleLabel.textColor = HLTextColor1;
    _subTitleLabel.font = UIFontMediumMake(14);
    _subTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_subTitleLabel];
    [_subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_versionLabel);
        make.top.equalTo(_versionLabel.mas_bottom).offset(8);
    }];
    
    self.contentLabel = [[UILabel alloc] init];
    _contentLabel.text = @"1.优化分享体验，试试把你喜欢的内容分享给好友吧！\n2.试试把你喜欢的内容分享给好友吧";
    _contentLabel.textColor = HLTextColor3;
    _contentLabel.font = UIFontMake(13);
    _contentLabel.numberOfLines = 0;
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    _contentLabel.qmui_lineHeight = 18;
    [self addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_versionLabel);
        make.top.equalTo(_subTitleLabel.mas_bottom).offset(8);
    }];
    
    self.okButton = [QMUIButton.alloc init];
    [_okButton setTitle:@"立即更新" forState:UIControlStateNormal];
    _okButton.titleLabel.font = UIFontMediumMake(16);
    [_okButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_okButton setBackgroundColor:HLMainColor];
    _okButton.layer.masksToBounds = YES;
    _okButton.layer.cornerRadius = 22;
    _okButton.hidden = YES;
    [self addSubview:_okButton];
    [_okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(20);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(-26);
    }];
    
    
    self.sureButton = [QMUIButton.alloc init];
    [_sureButton setTitle:@"立即更新" forState:UIControlStateNormal];
    _sureButton.titleLabel.font = UIFontMake(14);
    [_sureButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_sureButton setBackgroundColor:HLMainColor];
    _sureButton.layer.masksToBounds = YES;
    _sureButton.layer.cornerRadius = 22;
    _sureButton.hidden = YES;
    [self addSubview:_sureButton];
    [_sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.left.equalTo(self.mas_centerX).offset(4);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(-26);
    }];
    
    self.cancelButton = [QMUIButton.alloc init];
    [_cancelButton setTitle:@"下次再说" forState:UIControlStateNormal];
    _cancelButton.titleLabel.font = UIFontMake(14);
    [_cancelButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    [_cancelButton setBackgroundColor:HLBackgroundColor];
    _cancelButton.layer.masksToBounds = YES;
    _cancelButton.layer.cornerRadius = 22;
    _cancelButton.hidden = YES;
    [self addSubview:_cancelButton];
    [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.equalTo(self.mas_centerX).offset(-4);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(-26);
    }];

    
}

- (void)setModel:(HLUpdateModel *)model
{
    _model = model;
    
    _versionLabel.text = model.channelVersion;
    _contentLabel.text = model.publishContent;
    _okButton.hidden = ![model.hasCoerce boolValue];
    _sureButton.hidden = [model.hasCoerce boolValue];
    _cancelButton.hidden = [model.hasCoerce boolValue];
}



+(void)requestUpdateWithBlock:(dispatch_block_t)block isCoerce:(BOOL)isCoerce
{
    HLUserOptionRequest *userOptionRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypeUpdate params:@{}];
    [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
        if (request.isSuccess){
            HLUpdateModel *model = [HLUpdateModel mj_objectWithKeyValues:request.result[@"data"]];

            BOOL update = [model.hasUpdate boolValue];
            if (isCoerce){
                update = [model.hasCoerce boolValue];
            }
            if (model != nil && update) {
                [self showWithViewWithModel:model block:block];
            } else {
                if (block) {
                    block();
                }
            }
        }
    } failure:nil];
}

+ (void)showWithViewWithModel:(HLUpdateModel *)model block:(dispatch_block_t)completion
{
    HLUpdateTipView *contentView = [[HLUpdateTipView alloc] initWithFrame:CGRectMake(0, 0, (SCREEN_WIDTH -76), 366)];
    contentView.model = model;
    QMUIModalPresentationViewController *modalViewController = [[QMUIModalPresentationViewController alloc] init];
    modalViewController.contentView = contentView;
    modalViewController.contentViewMargins = UIEdgeInsetsZero;
    modalViewController.maximumContentViewWidth = SCREEN_WIDTH;
    modalViewController.animationStyle = QMUIModalPresentationAnimationStyleSlide;
    modalViewController.modal = [model.hasCoerce boolValue];
    modalViewController.didHideByDimmingViewTappedBlock = ^{
        if (completion) {
            completion();
        }
    };
    // 以 UIWindow 的形式来展示
    [modalViewController showWithAnimated:YES completion:nil];
    [contentView.okButton setQmui_tapBlock:^(__kindof UIControl *sender) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:model.downloadLink] options:@{} completionHandler:nil];
        
        if (![model.hasCoerce boolValue]) {
            if (completion) {
                completion();
            }
            [modalViewController hideWithAnimated:YES completion:nil];
        }
    }];
    
    [contentView.cancelButton setQmui_tapBlock:^(__kindof UIControl *sender) {
        [modalViewController hideWithAnimated:YES completion:nil];
    }];
    
    [contentView.sureButton setQmui_tapBlock:^(__kindof UIControl *sender) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:model.downloadLink] options:@{} completionHandler:nil];
        
        if (completion) {
            completion();
        }
        [modalViewController hideWithAnimated:YES completion:nil];
    }];
}


@end
